package com.employee.attendance_daka.controller;

import com.employee.attendance_daka.common.ResponseResult;
import com.employee.attendance_daka.domain.Attendance;
import com.employee.attendance_daka.service.AttendanceService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.format.annotation.DateTimeFormat;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDateTime;

@RestController
@RequestMapping("/attendance")
@CrossOrigin
public class AttendanceController {
    @Autowired
    private AttendanceService attendanceService;

    /**
     * 添加考勤信息 - 支持JSON请求体
     * @param attendance 包含员工ID和员工姓名等信息的Attendance对象
     * @return 响应结果，包含操作成功或失败的信息
     */
    @PostMapping("/add")
    public ResponseResult addAttendance(@RequestBody Attendance attendance) {
        // 参数校验
        if (attendance.getEmployeeId() == null || attendance.getEmployeeId().trim().isEmpty()) {
            return ResponseResult.error(400, "员工ID不能为空");
        }
        if (attendance.getEmployeeName() == null || attendance.getEmployeeName().trim().isEmpty()) {
            return ResponseResult.error(400, "员工姓名不能为空");
        }

        // 调用Service层方法
        String result = attendanceService.addAttendance(
                attendance.getEmployeeId(),
                attendance.getEmployeeName(),
                LocalDateTime.now(),
                null,
                attendance.getRemark()
        );

        // 根据打卡结果返回响应结果
        if(result.contains("打卡成功")){
            return ResponseResult.success(result);
        } else if(result.equals("打卡失败")){
            return ResponseResult.error(400, result);
        } else{
            return ResponseResult.error(500, result);
        }
    }
    /**
     * 删除考勤信息
     * @param id 要删除的考勤信息的ID
     * @return 响应结果，包含操作成功或失败的信息
     */
    @DeleteMapping("/delete/{id}")
    public ResponseResult deleteAttendance(@PathVariable int id) {
        // 调用Service层方法
        String result = attendanceService.deleteAttendance(id);

        // 根据删除结果返回响应结果
        if(result.equals("删除成功")){
            return ResponseResult.success(result);
        }else if(result.equals("删除失败")){
            return ResponseResult.error(400, result);
        } else{
            return ResponseResult.error(500, result);
        }
    }

    /**
     * 修改考勤信息
     * @param id 要修改的考勤信息的ID
     * @param remark 新的备注信息
     * @return 响应结果，包含操作成功或失败的信息
     */
    @PutMapping("/update/{id}")
    public ResponseResult updateAttendance(@PathVariable int id, @RequestParam(required = false) String remark) {
        // 调用Service层方法
        String result = attendanceService.updateAttendance(id, remark);

        // 根据修改结果返回响应结果
        if(result.equals("更新成功")){
            return ResponseResult.success(result);
        }else if(result.equals("更新失败")){
            return ResponseResult.error(400, result);
        } else{
            return ResponseResult.error(500, result);
        }
    }
    /**
     * 获取考勤信息
     * @param employeeId 员工ID
     * @param employeeName 员工姓名
     * @param startTime 开始时间
     * @param endTime 结束时间
     * @return 响应结果，包含考勤信息列表
     */
    @GetMapping("/get")
    public ResponseResult getAttendance(@RequestParam(required = false) String employeeId,
                                        @RequestParam(required = false) String employeeName,
                                        @RequestParam(required = false) @DateTimeFormat(pattern = "yyyy-MM-dd HH:mm:ss") LocalDateTime startTime,
                                        @RequestParam(required = false) @DateTimeFormat(pattern = "yyyy-MM-dd HH:mm:ss") LocalDateTime endTime){
        return ResponseResult.success(attendanceService.getAttendance(employeeId, employeeName, startTime, endTime));
    }
}